package com.library.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.library.dao.DAOFactory;
import com.library.dao.MySQLDAOFactory;
import com.library.dao.User;
import com.library.dao.UserDAO;

/**
 * Servlet implementation class LibrarianGetInfor
 */
@WebServlet("/LibrarianGetInfor")
public class LibrarianGetInfor extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LibrarianGetInfor() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			String userId = request.getParameter("id");
			int id = Integer.parseInt(userId);
			DAOFactory factory = new MySQLDAOFactory();
			UserDAO dao = factory.getUserDAO();
			User us = dao.findUser(id);
			
			response.setContentType("text/xml;charset=UTF-8");
		    PrintWriter out = response.getWriter();
		    
		    out.println("<?xml version=\"1.0\"?>");
		    out.println("<kh>");
			if (us != null){
				out.println("<user>");
				
				out.println("<password>");
				out.println(us.getPassword());
				out.println("</password>");
				
				out.println("<email>");
				out.println(us.getEmail());
				out.println("</email>");
				
				out.println("<phone>");
				out.println(us.getPhone());
				out.println("</phone>");
				
				out.println("<name>");
				out.println(us.getName());
				out.println("</name>");
				
				out.println("<address>");
				out.println(us.getAddress());
				out.println("</address>");
				out.println("</user>");
			}
			out.println("</kh>");
		} catch(Exception e){
			response.setContentType("text/xml;charset=UTF-8");
		    PrintWriter out = response.getWriter();
		    out.println("<?xml version=\"1.0\"?>");
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
